package org.wanghong.basic.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.wanghong.basic.mapper.BaseMapper;
import org.wanghong.basic.query.BaseQuery;
import org.wanghong.basic.service.IBaseService;
import org.wanghong.basic.util.PageList;

import java.io.Serializable;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> mapper;

    @Transactional
    @Override
    public void add(T t) {
        mapper.save(t);
    }

    @Transactional
    @Override
    public void del(Serializable id) {
        mapper.remove(id);
    }
    @Transactional
    @Override
    public void update(T t) {
    mapper.update(t);
    }

    @Override
    public T getById(Serializable id) {
        return mapper.loadById(id);
    }


    @Override
    public List<T> getAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        long total = mapper.loadCount(query);
        if (total<1)
            return new PageList<>();
        List<T> rows = mapper.loadData(query);
        return new PageList<>(total,rows);
    }
}
